let dbConfig = require('../utils/dbConfig')
let fs = require('fs')
// 获取用户
getUsers = (req, res) => {
  let sql = "select * from user"
  let sqlArr = []
  let callBack = (err, data) => {
    if (err) {
      console.log("连接出错了")
    } else {
      res.send({
        'code': 200,
        'data': data
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//获取各分院注册人数
getNumByCollege = (req, res) => {
  let sql = 'select college as type,count(college) as num from user group by college'
  let sqlArr = []
  let callBack = (err, data) => {
    if (err) {
      console.log("连接出错了")
    } else {
      res.send({
        'code': 200,
        'data': data
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
// 认证结果
saveCertification = async (req, res) => {
  let { userID, judge } = req.body
  console.log(userID, judge);
  if (judge === 'pass') {
    judge = 'true'
  }
  let sql = 'update user set certified=? where userID=?'
  let sqlArr = [judge, userID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': '认证成功'
    })
  } else {
    res.send({
      'code': 400,
      'msg': '认证失败'
    })
  }
}
// 获取当日和总校友圈数量
getMomentNum = async (req, res) => {
  let today = new Date()
  let time = (new Date(today.toDateString())).getTime()
  let sql = 'select count(*) as todayCount from moment where time between ? and ?'
  let sqlArr = [time.toString(), (time + 86400000).toString()]
  let conta = { todayCount: 0, totalCount: 0 }
  let callBack = (err, data) => {
    if (err) {
      console.log("连接出错了", err)
    } else {
      conta.todayCount = data[0].todayCount
    }
  }
  await dbConfig.sqlConnect(sql, sqlArr, callBack)
  sql = 'select count(*) as totalCount from moment'
  sqlArr = []
  callBack = (err, data) => {
    if (err) {
      console.log("连接出错了", err)
    } else {
      conta.totalCount = data[0].totalCount
      res.send({
        'code': 200,
        'data': conta
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
// 获取当日和总订单数量
getOrderNum = async (req, res) => {
  let today = new Date()
  let time = (new Date(today.toDateString())).getTime()
  let sql = 'select count(*) as todayCount from user_order where create_time between ? and ?'
  let sqlArr = [time.toString(), (time + 86400000).toString()]
  let conta = { todayCount: 0, totalCount: 0 }
  let callBack = (err, data) => {
    if (err) {
      console.log("连接出错了", err)
    } else {
      conta.todayCount = data[0].todayCount
    }
  }
  await dbConfig.sqlConnect(sql, sqlArr, callBack)
  sql = 'select count(*) as totalCount from user_order'
  sqlArr = []
  callBack = (err, data) => {
    if (err) {
      console.log("连接出错了", err)
    } else {
      conta.totalCount = data[0].totalCount
      res.send({
        'code': 200,
        'data': conta
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
getRegisterNum = async (req, res) => {
  let today = new Date()
  let time = (new Date(today.toDateString())).getTime()
  let sql = 'select count(*) as todayCount from user where create_time between ? and ?'
  let sqlArr = [time.toString(), (time + 86400000).toString()]
  let conta = { todayCount: 0, totalCount: 0 }
  let callBack = (err, data) => {
    if (err) {
      console.log("连接出错了", err)
    } else {
      conta.todayCount = data[0].todayCount
    }
  }
  await dbConfig.sqlConnect(sql, sqlArr, callBack)
  sql = 'select count(*) as totalCount from user'
  sqlArr = []
  callBack = (err, data) => {
    if (err) {
      console.log("连接出错了", err)
    } else {
      conta.totalCount = data[0].totalCount
      res.send({
        'code': 200,
        'data': conta
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
delCarousel = async (req, res) => {
  let { carouselID } = req.query
  let sql = 'delete from carousel where carouselID=?'
  let sqlArr = [carouselID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': '删除成功'
    })
  } else {
    res.send({
      'code': 400,
      'msg': '删除失败'
    })
  }
}
updateCarousel = async (req, res) => {
  let { carouselID } = req.body
  let file = req.file
  fs.renameSync('./public/uploads/carousel/' + file.filename, './public/uploads/carousel/' + file.originalname)
  let fileName = `http://localhost:3000/uploads/carousel/${file.originalname}`
  let sql = 'update carousel set src=? where carouselID=?'
  let sqlArr = [fileName, carouselID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': '更新成功'
    })
  } else {
    res.send({
      'code': 400,
      'msg': '更新失败'
    })
  }
}
addCarousel = async (req, res) => {
  let file = req.file
  fs.renameSync('./public/uploads/carousel/' + file.filename, './public/uploads/carousel/' + file.originalname)
  let fileName = `http://localhost:3000/uploads/carousel/${file.originalname}`
  let sql = 'insert into carousel(src) values (?)'
  let sqlArr = [fileName]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': '添加成功'
    })
  } else {
    res.send({
      'code': 400,
      'msg': '添加失败'
    })
  }
}
saveNotice = async (req, res) => {
  console.log(req.body)
  let { title, publisher, content } = req.body
  let sql = 'insert into notice(author,title,content,type,time) values(?,?,?,?,?)'
  let sqlArr = [publisher, title, content, "0", new Date()]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows && result.affectedRows === 1) {
    res.send({
      'code': 200,
      'msg': "提交成功"
    })
  }else{
    res.send({
      'code': 400,
      'msg': "提交失败"
    })
  }

}
getMoment = (req, res) => {
  let sql = 'select * from moment'
  let sqlArr = []
  var callBack = (err, data) => {
    if (err) {
      console.log("连接出错了")
    } else {
      res.send({
        'code': 200,
        'data': data,
        'msg': "获取数据成功"
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
shieldMoment = async (req, res) => {
  let { momentID } = req.body
  let sql = 'update moment set status=-1 where momentID=?'
  let sqlArr = [momentID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'tip': '屏蔽成功'
    })
  } else {
    res.send({
      'code': 400,
      'tip': '屏蔽失败'
    })
  }
}
createMomentType = async (req, res) => {
  let { name } = req.body
  let sql = 'insert into moment_type(name,create_time) values(?,?)'
  let sqlArr = [name, (new Date()).valueOf()]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'tip': '创建成功'
    })
  } else {
    res.send({
      'code': 400,
      'tip': '创建失败'
    })
  }
}
delMomentType = async (req, res) => {
  let { ID } = req.body
  let sql = 'delete from moment_type where m_typeID=?'
  let sqlArr = [ID]
  let result = await dbConfig.SySqlConnect(sql, sqlArr)
  if (result.affectedRows === 1) {
    res.send({
      'code': 200,
      'tip': '删除成功'
    })
  } else {
    res.send({
      'code': 400,
      'tip': '删除失败'
    })
  }
}
module.exports = {
  getUsers,
  saveCertification,
  getMomentNum,
  getOrderNum,
  getRegisterNum,
  getNumByCollege,
  delCarousel,
  updateCarousel,
  addCarousel,
  saveNotice,
  getMoment,
  shieldMoment,
  createMomentType,
  delMomentType
}